#!/usr/bin/evn python
# -*- coding: utf-8 -*-
"""
description: please input using description
author: justbk
date: 2022/1/26
modify_records:
    - 2022/1/26 justbk create this file
"""

import os, sys

IPS="""20.20.20.52
20.20.20.54
20.20.20.56
20.20.20.58
20.20.20.60
20.20.20.62
20.20.20.67
20.20.20.70
20.20.20.73
20.20.20.76
20.20.20.79
20.20.20.82
20.20.20.85
20.20.20.88"""
title="""<?xml version='1.1' encoding='UTF-8'?>
<jenkins.plugins.publish__over__ssh.BapSshPublisherPlugin_-Descriptor plugin="publish-over-ssh@1.22">
  <hostConfigurations>
"""
bottom="""  </hostConfigurations>
  <commonConfig reference="../hostConfigurations/jenkins.plugins.publish__over__ssh.BapSshHostConfiguration/commonConfig"/>
  <defaults class="jenkins.plugins.publish_over_ssh.options.SshPluginDefaults"/>
</jenkins.plugins.publish__over__ssh.BapSshPublisherPlugin_-Descriptor>
"""
ssh_pub = """    <jenkins.plugins.publish__over__ssh.BapSshHostConfiguration>
      <name>###</name>
      <hostname>$$$</hostname>
      <username>peilq</username>
      <secretPassword>{AQAAABAAAAAQRLnncAssGYLTRoAoFFA6ZC8lGJCIBn6CRQoTECbSRx4=}</secretPassword>
      <remoteRootDir>/home/peilq/ci/test</remoteRootDir>
      <port>22</port>
      <commonConfig class="jenkins.plugins.publish_over_ssh.BapSshCommonConfiguration" reference="../../jenkins.plugins.publish__over__ssh.BapSshHostConfiguration/commonConfig"/>
      <timeout>300000</timeout>
      <overrideKey>true</overrideKey>
      <disableExec>false</disableExec>
      <keyInfo>
        <secretPassphrase>{AQAAABAAAABA7CPzjjPhQe+y3e6v8+eNir91EPJ0ee3v+qzEwHZAYAID1A9jYR1Pjhdj/6QJgFOB8iRiyUaGZcLyXaqPnjjFHTJmi5yx5cOOagGxKoATjnA=}</secretPassphrase>
        <key></key>
        <keyPath></keyPath>
      </keyInfo>
      <jumpHost></jumpHost>
      <proxyType></proxyType>
      <proxyHost></proxyHost>
      <proxyPort>0</proxyPort>
      <proxyUser></proxyUser>
      <proxyPassword></proxyPassword>
    </jenkins.plugins.publish__over__ssh.BapSshHostConfiguration>
"""

ips = IPS.split("\n")
class CreateSSHConfig:
    @classmethod
    def run(cls):
        cur_path = os.path.join(os.path.dirname(__file__), "jenkins.plugins.publish_over_ssh.BapSshPublisherPlugin.xml")
        with open(cur_path, "wt") as f:
            f.write(title)
            for ip in ips:
                cur = ssh_pub.replace("$$$", ip).replace("###", ip.split(".")[-1])
                f.write(cur)
            f.write(bottom)

class TagGenerate:
    NAMES = ["DB", "PROXY"]
    TAG = "string"

    @classmethod
    def get_name_tag(cls, index, name):
        return "{0}_{1}".format(name, index)

    @classmethod
    def get_ip_tag(cls, index, name):
        return "{0}_{1}_IP".format(name, index)

    @classmethod
    def run(cls):
        for name in cls.NAMES:
            for idx, ip in enumerate(ips):
                print("<{0}>{1}</{0}>".format(cls.TAG, cls.get_name_tag(idx, name)))
                print("<{0}>{1}</{0}>".format(cls.TAG, "{0}{1}".format(name.lower(), ip.split(".")[-1])))
                print("<{0}>{1}</{0}>".format(cls.TAG, cls.get_ip_tag(idx, name)))
                print("<{0}>{1}</{0}>".format(cls.TAG, ip))

#TagGenerate.run()
CreateSSHConfig.run()
